<?php

namespace App\Model;


use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class SuggestRoomList extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'suggest_room_list'; //首页推荐房间
    protected $roomTable = 'room';//房间表


    //搜索房间
    public function searchRoom($kwd, $page=1, $limit=10){
        //根据房间uid查询roomid
        $roomInfo = DB::connection($this->connection)->table($this->roomTable)->where('room_uid',$kwd)->first();
        $roomInfo = objToArray($roomInfo);
        $data = [];
        if(!empty($roomInfo)){
            $room_id = $roomInfo['room_id'];
            $data = DB::connection($this->connection)->table($this->table)->where(['is_deleted'=>0,'room_id'=>$room_id])->paginate($limit,['*'],'page',$page);
            $data = objToArray($data);
            foreach($data['data'] as &$v){
                //查询房间名称
                $roomInfo = DB::connection($this->connection)->table($this->roomTable)->where('room_id',$v['room_id'])->first();
                $roomInfo = objToArray($roomInfo);
                $v['roomName'] = empty($roomInfo['name']) ? '' : $roomInfo['name'];
                $v['start_timestamp'] = Carbon::parse($v['start_timestamp'])->toDateTimeString();
                $v['end_timestamp'] = Carbon::parse($v['end_timestamp'])->toDateTimeString();
            }
        }
        return $data;
    }

    //获取推荐房间列表
    public function getSuggestList($page=1, $limit=10){
        $list = DB::connection($this->connection)->table($this->table)->where('is_deleted',0)->orderBy('sort','ASC')->paginate($limit,['*'],'page',$page);
        $list = objToArray($list);
        if(!empty($list['data'])){
            foreach($list['data'] as &$v){
                //查询房间名称
                $roomInfo = DB::connection($this->connection)->table($this->roomTable)->where('room_id',$v['room_id'])->first();
                $roomInfo = objToArray($roomInfo);
                $v['roomName'] = empty($roomInfo['name']) ? '' : $roomInfo['name'];
                $v['start_timestamp'] = date('Y-m-d H:i:s',$v['start_timestamp']);
                $v['end_timestamp'] = date('Y-m-d H:i:s',$v['end_timestamp']);
            }
        }
        return $list;
    }

    //更改显示状态
    public function changeStatus($chageid, $showStatus, $isDelete){
        if(!is_null($showStatus)){
            $param['show_status'] = $showStatus;
        }
        if(!is_null($isDelete)){
            $param['is_delete'] = $isDelete;
        }
        $param['id'] = $chageid;
        $status = curl_request(getKkaudioApiUrl() . '/inner/roomsuggest/chagestatus', $param, 'json', 'POST');
        return $status;
    }

    //添加或修改数据
    public function addOrUpdate($id, $room_id, $start_time, $end_time, $sort, $is_show){
        $param = [
            'id' => $id,//编辑id
            'room_id' => $room_id,//房间id
            'start_time' => strtotime($start_time),//开始时间
            'end_time' => strtotime($end_time),//结束时间
            'sort' => $sort,//排序
            'is_show' => $is_show,//是否显示1显示0不显示
        ];
        $status = curl_request(getKkaudioApiUrl() . '/inner/roomsuggest/addorupdate', $param, 'json', 'POST');
        return $status;
    }
}